package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"time"
)

type IpccLostRepair struct {
	Id         int
	Number     string
	SessionId  string
	CreateTime time.Time
}

func SaveLostToDb(number, sessionId string) bool {

	newLost := &IpccLostRepair{
		Number:     number,
		SessionId:  sessionId,
		CreateTime: time.Now(),
	}

	o := orm.NewOrm()
	o.Using("default")

	_, err := o.Insert(newLost)
	if err != nil {
		beego.Error("insert to db failed :%v,success:%v,want to:%v!", err)
		return false
	}

	return true
}

func GetLostSessionId(number string) (sessionId string, err error) {

	newLost := &IpccLostRepair{
		Number: number,
	}

	o := orm.NewOrm()
	o.Using("default")

	err = o.QueryTable(new(IpccLostRepair)).Filter("Number", number).OrderBy("-CreateTime").One(newLost)
	sessionId = newLost.SessionId

	return
}

func RegIpccLostRepair() {
	orm.RegisterModel(new(IpccLostRepair))
}
